﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Domain.Repositories;
using Abp.ElasticSearch;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using FwProject.AppManager;
using FwProject.Keyword.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Keyword.Dto;
using Nest;
using X.PagedList;

namespace FwProject.Keyword
{
    public class KeywordAppService : FwProjectAppServiceBase, IKeywordAppService
    {
        private readonly IRepository<KeywordInfo, Guid> _repositoryKeyword;
        public KeywordInfoManager _keywordInfoManager;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }
        /// <summary>
        /// 搜索引擎
        /// </summary>
        private readonly IElasticsearch _elasticsearch;
        public KeywordAppService(IRepository<KeywordInfo, Guid> repositoryKeyword, IElasticsearch elasticsearch, KeywordInfoManager keywordInfoManager)
        {
            _elasticsearch = elasticsearch;
            _repositoryKeyword = repositoryKeyword;
            _keywordInfoManager = keywordInfoManager;
        }
        /// <summary>
        /// 获取一个关键词
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public KeywordAppDto GetByIdAppKeyWord(Guid id) {

          return  _repositoryKeyword.GetAll().Where(o => o.isActive == true && o.Id==id).ProjectTo<KeywordAppDto>().FirstOrDefault();
        }
        public KeywordAppDto GetDeleteByIdAppKeyWord(Guid id)
        {
            return _repositoryKeyword.GetAll().Where(o =>  o.Id == id).ProjectTo<KeywordAppDto>().FirstOrDefault();
        }
        /// <summary>
        /// 搜索引擎返回关键词
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<List<KeywordAppDto>> GetSearchAppKeyWord(int page, int pageSize)
        {
            SearchDescriptor<KeywordAppDto> tquery = new SearchDescriptor<KeywordAppDto>();
            tquery = tquery.MatchAll();
            page = page-1;
            var model = await _elasticsearch.SearchAsync<KeywordAppDto, Guid>(nameof(KeywordAppDto).ToLower(), tquery, page, pageSize);
            var list = model.Documents;
            return list.ToList();

        }

        public async Task<List<KeywordAppDto>> GetPagedListKeyWordAsync(int page, int limit, string keyword)
        {

             SearchDescriptor<KeywordAppDto> tquery = new SearchDescriptor<KeywordAppDto>();
             tquery = tquery.Query(q =>q.Bool(b=>b.Must(m=>m.QueryString(k=>k.Query(keyword).Type(TextQueryType.MostFields)))));
             page= page-1;
             var model = await _elasticsearch.SearchAsync<KeywordAppDto, Guid>(nameof(KeywordAppDto).ToLower(), tquery, page, limit);
             var list = model.Documents;
             return list.ToList();
        }

        #region 后台管理
        /// <summary>
        /// 添加关键字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Create(KeywordInput input)
        {
            var model = _map.Map<KeywordInfo>(input);
            model.Id = _guid.Create();

            return await _keywordInfoManager.CreateKeyword(model);
        }
        /// <summary>
        /// 编辑关键字
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task Edit(Guid id, KeywordInput input)
        {
            var model = await _repositoryKeyword.GetAsync(id);
            model = _map.Map(input, model);
            await _keywordInfoManager.UpdateKeyword(model);
        }
        /// <summary>
        /// 修改启用状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="active"></param>
        /// <returns></returns>
        public async Task EditisActive(Guid id, bool active)
        {
            var model = await _repositoryKeyword.GetAsync(id);
            model.isActive = active;
            await _keywordInfoManager.UpdateKeyword(model);
        }

        /// <summary>
        /// 删除关键字
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task Delete(Guid id)
        {
            var model = await _repositoryKeyword.GetAsync(id);
            await _keywordInfoManager.DeleteKeyword(model);
        }
        /// <summary>
        /// 获取一条关键字信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<KeywordDto> GetByIdAsync(Guid id)
        {
            var model = await _repositoryKeyword.GetAsync(id);
            return _map.Map<KeywordDto>(model);
        }

        /// <summary>
        /// 获取关键字列表
        /// </summary>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页显示条数</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public async Task<LayerDataBaseDto> GetPagedListAsync(int page,int limit,string keyword)
        {
            var listmodel = _repositoryKeyword.GetAll();
            if (!string.IsNullOrEmpty(keyword))
                listmodel = listmodel.Where(o=>o.KeywordTitle.Contains(keyword));

            var pagelist = await listmodel.OrderByDescending(o => o.CreationTime).ProjectTo<KeywordDto>().ToPagedListAsync(page,limit);

            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = pagelist;
            dto.Count = pagelist.TotalItemCount;
            return dto;
        }
        #endregion
    }
}
